import { readFileSync, readdirSync, writeFileSync } from 'fs'
import { resolve, join } from 'path'

const srcDir = resolve('./src')
const srcIndexFilePath = join(srcDir, 'index.ts')
const componentsDir = join(srcDir, 'components')

const indexText = readFileSync(srcIndexFilePath, 'utf-8')
const componentsText = readdirSync(componentsDir).filter(a => a.endsWith('.vue')).sort().map(a => {
    let componentName = a.split('.')[0]
    return `export { default as ${componentName} } from '@/components/${componentName}.vue'`
}).join('\n')
const newIndexText = indexText.replace(/\/\/ components start[\s\S]*\/\/ components end/, '// components start\n' + componentsText + '\n// components end')
writeFileSync(srcIndexFilePath, newIndexText)